package com.q.springboot.project.service.impl;

import com.q.springboot.framework.util.JsonUtils;
import com.q.springboot.project.dao.TUserDao;
import com.q.springboot.project.entity.TUser;
import com.q.springboot.project.event.LogEvent;
import com.q.springboot.project.service.TUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TUser)表服务实现类
 *
 * @author makejava
 * @since 2020-06-21 19:18:13
 */
@Service("tUserService")
@Slf4j
public class TUserServiceImpl implements TUserService {
    @Resource
    private TUserDao tUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TUser queryById(Integer id) {
        return this.tUserDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<TUser> queryAllByLimit(int offset, int limit) {
        return this.tUserDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param tUser 实例对象
     * @return 实例对象
     */
    @Override
    public TUser insert(TUser tUser) {
        this.tUserDao.insert(tUser);
        return tUser;
    }

    /**
     * 修改数据
     *
     * @param tUser 实例对象
     * @return 实例对象
     */
    @Override
    public TUser update(TUser tUser) {
        this.tUserDao.update(tUser);
        return this.queryById(tUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.tUserDao.deleteById(id) > 0;
    }

    @Override
    public List<TUser> queryAll(TUser tUser) {
        return this.tUserDao.queryAll(tUser);
    }

    @Override
    @Async("asyncServiceExecutor")
    public void async() throws InterruptedException {
        Thread.sleep(10_000);
        log.info("哈哈哈哈");
    }

    @Override
    @Async
    @EventListener(LogEvent.class)
    public void logEvent(LogEvent logEvent) {
        log.info("logEvent: {}", JsonUtils.objectToJson(logEvent));
        log.info("logEvent.Source: {}", JsonUtils.objectToJson(logEvent.getSource()));
    }

    @Override
    public TUser tselect(String id) {
        return tUserDao.tselect(id);
    }
}
